package com.py.credit.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.service.DownloadService;
import com.py.credit.utils.FTPUtil;
import com.py.framework.core.common.Page;

/**
 * @Company: WWW.xypy.com.cn
 * @ClassName FtpController
 * @Description: FTP大文件下载类
 * @Author: YaoS
 * @Create: 2018-09-20 17:08
 **/
@RestController
@RequestMapping(value = "/ftp")
public class DownloadController {
	@Autowired
	public FTPUtil ftpUtil;

	@Autowired
	private DownloadService downloadService;

	public static Map<String, Object> map = new HashMap<>();

	@RequestMapping(value = "/selectDown")
	public RestServiceResult<List<Map<String, Object>>> selectDownForPage(String type, Page page) throws Exception {
		RestServiceResult<List<Map<String, Object>>> result = new RestServiceResult<List<Map<String, Object>>>();
		try {
			result.setData(downloadService.selectDownForPage(type, page));
			result.setCount(page.getTotalCount());
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("数据异常！ERROR:" + e.getMessage());
			ExceptionLogger.error("查询下载文件列表异常！", "查询下载文件列表异常！ERROR:" + e.getMessage(), e);
		}
		return result;
	}

	/**
	 * @return
	 * @Author YaoS
	 * @Description //TODO
	 * @Date 14:13 18/9/27
	 * @Param 返回FTP服务器信息
	 **/
	@RequestMapping(value = "/selectFTP")
	public RestServiceResult<Map<String, Object>> selectFTP(String type) throws Exception {
		map.put("username", ftpUtil.getUsername());
		map.put("password", ftpUtil.getPassword());
		map.put("host", ftpUtil.getHost());
		map.put("port", ftpUtil.getPort());

		RestServiceResult<Map<String, Object>> result = new RestServiceResult<Map<String, Object>>();
		try {
			result.setData(map);
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("数据异常！ERROR:" + e.getMessage());
			ExceptionLogger.error("查询下载文件列表异常！", "查询下载文件列表异常！ERROR:" + e.getMessage(), e);
		}
		return result;

	}

	/**
	 * @return
	 * @Author YaoS
	 * @Description //TODO
	 * @Date 19:26 18/9/27
	 * @Param 定时任务将ftp文件夹内容同步更新
	 **/
	@RequestMapping(value = "/updateFTP.do")
	public RestServiceResult<Map<String, Object>> updateFTP() throws Exception {
		RestServiceResult<Map<String, Object>> result = new RestServiceResult<Map<String, Object>>();
		ArrayList<String> arrayList = new ArrayList<>();
		String pathname = "/";
		try {
			if (ftpUtil.login()) {
				arrayList = ftpUtil.list(pathname);
			}
			downloadService.updateFTP(arrayList);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("list", arrayList);
			result.setData(map);
			result.setCode(RestServiceResult.CODE_SUCCESS);
		} catch (Exception e) {
			ExceptionLogger.error(e);
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("ftp更新失败！");
		}
		return result;
	}

	/**
	 * @return
	 * @Author YaoS
	 * @Description //TODO
	 * @Date 19:26 18/9/27
	 * @Param 下载统计+1
	 **/
	@RequestMapping(value = "/updateSUM")
	public RestServiceResult<Object> updateSUM(String name, String type) {

		RestServiceResult<Object> result = new RestServiceResult<Object>();
		try {
			downloadService.updateSUM(name, type);
			result.setData("true");
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("数据异常！ERROR:" + e.getMessage());
			ExceptionLogger.error("查询下载文件列表异常！", "查询下载文件列表异常！ERROR:" + e.getMessage(), e);
		}
		return result;
	}

}
